package com.karry.yl.web.devices;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.devices.Devices;
import com.karry.yl.service.devices.DevicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 床位信息Controller
 */
@Controller
@RequestMapping("/devices")
public class DevicesController extends SuperController {

    @Autowired
    private DevicesService devicesService;

    /**
     * 到楼宇列表页
     *
     * @return
     */
    @RequestMapping
    public String listUI() {
        return "devices/device/list";
    }

    @RequestMapping("/add")
    public String addUI() {
        return "devices/device/add";
    }

    @RequestMapping("edit")
    public String editUI(Long id, ModelMap map) {
        Devices devices = devicesService.selectById(id);
        map.put("devices", devices);
        return "devices/device/edit";
    }


    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit) {
        Page<Devices> devicesPage = devicesService.selectPage(new Page<Devices>(page, limit), new EntityWrapper<Devices>().eq("orgId", currentUser.getOrgId()).orderBy("createdTime",false));
        return new LayTableResponse(devicesPage.getRecords(), devicesPage.getTotal());
    }

    @RequestMapping("/save")
    public String save(Devices devices) {
        devices.setOrgId(currentUser.getOrgId());
        devicesService.insert(devices);
        return "redirect:/devices";
    }

    @RequestMapping("/update")
    public String update(Devices devices) {
        devices.setOrgId(currentUser.getOrgId());
        devicesService.updateById(devices);
        return "redirect:/devices";
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids) {
        devicesService.deleteBatchIds(ids);
        return "redirect:/devices";
    }
}
